package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/15 13:01
 **/
public class Day30_1_Num641_MyCircularDeque {
    class MyCircularDeque {
        int[] data;
        int index = -1;

        public MyCircularDeque(int k) {
            data = new int[k];
        }

        public boolean insertFront(int value) {
            if(isFull()){
                return false;
            }
            for(int i = index+1;i > 0;i--){
                data[i] = data[i-1];
            }
            data[0] = value;
            index++;
            return true;
        }

        public boolean insertLast(int value) {
            if(isFull()){
                return false;
            }
            index++;
            data[index] = value;
            return true;
        }

        public boolean deleteFront() {
            if(isEmpty()){
                return false;
            }
            for(int i = 0;i < index;i++){
                data[i] = data[i+1];
            }
            index--;
            return true;
        }

        public boolean deleteLast() {
            if(isEmpty()){
                return false;
            }
            data[index] = 0;
            index--;
            return true;
        }

        public int getFront() {
            if(isEmpty()) return -1;
            return data[0];
        }

        public int getRear() {
            if(isEmpty()) return -1;
            return data[index];
        }

        public boolean isEmpty() {
            if(index == -1){
                return true;
            }
            return false;
        }

        public boolean isFull() {
            if(index == data.length-1){
                return true;
            }
            return false;
        }
    }

    class MyCircularDeque1 {
        int[] data;
        int head, tail;
        int capacity;

        public MyCircularDeque1(int k) {
            capacity = k+1;
            data = new int[capacity];
            head = tail = 0;
        }

        public boolean insertFront(int value) {
            if(isFull()) return false;
            head = (head-1 + capacity)%capacity;
            data[head] = value;
            return true;
        }

        public boolean insertLast(int value) {
            if(isFull()) return false;
            data[tail] = value;
            tail = (tail+1)%capacity;
            return true;
        }

        public boolean deleteFront() {
            if(isEmpty()) return false;
            head = (head+1)%capacity;
            return true;
        }

        public boolean deleteLast() {
            if(isEmpty()) return false;
            tail = (tail-1 + capacity)%capacity;
            return true;
        }

        public int getFront() {
            if(isEmpty()) return -1;
            return data[head];
        }

        public int getRear() {
            if(isEmpty()) return -1;
            return data[(tail-1 + capacity)%capacity];
        }

        public boolean isEmpty() {
            return head == tail;
        }

        public boolean isFull() {
            return head == ((tail+1)%capacity);
        }
    }
}